﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage;
using mozhi.smarterp.Finance.Accounts.Repository;
using mozhi.smarterp.Finance.EntityFrameworkCore;
using mozhi.smarterp.Finance.Enums;
using mozhi.smarterp.Finance.Querys;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Linq;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;

namespace mozhi.smarterp.Finance.Accounts
{
    public class AccountDimensionRepository : EfCoreRepository<FinanceDbContext, AccountDimension , Guid>, IAccountDimensionRepository
    {
        public AccountDimensionRepository(IDbContextProvider<FinanceDbContext> dbContextProvider) : base(dbContextProvider)
        {
        }

        public async Task<List<AccountDimension>> FindAsync(PageQuery query)
        {
            var dimensions = await DbContext.Set<AccountDimension>().AsNoTracking()
                .WhereIf(!query.Keyword.IsNullOrEmpty(), x => x.Code.Contains(query.Keyword) || x.Name.Contains(query.Keyword))
                .WhereIf(query.Ids != null && query.Ids.Count > 0, x => query.Ids.Contains(x.Id))
                .WhereIf(query.Activated.HasValue, x => x.Activated == query.Activated.Value)
                .Skip((query.PageIndex - 1) * query.PageSize).Take(query.PageSize)
                .ToListAsync();

            return dimensions;
        }

        public async Task<int> GetCountAsync(PageQuery query)
        {
            int count = await DbContext.Set<AccountDimension>().AsNoTracking()
                 .WhereIf(!query.Keyword.IsNullOrEmpty(), x => x.Code.Contains(query.Keyword) || x.Name.Contains(query.Keyword))
                 .WhereIf(query.Activated.HasValue, x => x.Activated == query.Activated.Value)
                 .WhereIf(query.ExclusionId.HasValue, x => x.Id != query.ExclusionId)
                 .CountAsync();

            return count;
        }
    }
}
